package com.android.plc.ui.manager;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.android.plc.R;
import com.android.plc.classholder.BasicItem;
import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.Room;
import com.android.plc.provider.PLC;
import com.android.plc.provider.PLC.Rooms;
import com.android.plc.ui.dialog.OnWheelSelectListener;
import com.android.plc.ui.dialog.RoomCodeDialog;
import com.android.plc.util.StringUtil;

public class RoomManager extends BaseManager<Room>
{
	private Button mOKBtn;
	private Button mDelBtn;
	private EditText mTxtName;
	private EditText mTxtCode;
	private int mCode = 0;

	public RoomManager(Context context, ViewGroup view)
	{
		super(context, view, R.layout.add_room);
	}

	@Override
	public void initView(ViewGroup controlView)
	{
		mOKBtn = (Button) controlView.findViewById(R.id.edit_save);
		mDelBtn = (Button) controlView.findViewById(R.id.edit_del);
		mTxtName = (EditText) controlView.findViewById(R.id.edit_room_name);
		mTxtCode = (EditText) controlView.findViewById(R.id.edit_room_code);

		setRoomCode(0);
		mTxtCode.setClickable(true);
		mTxtCode.setOnFocusChangeListener(new OnFocusChangeListener()
		{
			public void onFocusChange(View v, boolean hasFocus)
			{
				if (hasFocus)
				{
					showDialog();
				}
			}
		});
		mTxtCode.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				showDialog();
			}
		});

		displayItem(getRoom());
		mOKBtn.setOnClickListener(this);
		mDelBtn.setOnClickListener(this);
		mTxtName.addTextChangedListener(this);
	}

	private void showDialog()
	{
		RoomCodeDialog dialog = new RoomCodeDialog(mContext);
		final Room room = getRoom();
		dialog.setSelectCodeListener(new OnWheelSelectListener()
		{
			public void onSelected(Object obj)
			{
				int result = (Integer) obj;
				if (room != null && room._id > 0 && room.code != result)
				{
					room.code = result;
					update(room);
				}
				setRoomCode(result);
			}
		});
		dialog.show();
		dialog.selectDeviceCode(room.code);
	}

	public void afterTextChanged(Editable s)
	{
		Room room = getRoom();
		String newRoomName = mTxtName.getText().toString().trim();
		boolean same = newRoomName.equals(room.name);
		if (room != null && room._id > 0 && checkValidity(getView()) && !same)
		{
			room.name = newRoomName;
			update(room);
		}
	}

	public Uri getUri()
	{
		return PLC.Rooms.CONTENT_URI;
	}

	@Override
	public ContentValues item2maps(Room room)
	{
		ContentValues values = new ContentValues();
		values.put(Rooms.NAME, room.name);
		values.put(Rooms.CODE, room.code);
		return values;
	}

	public void onClick(View arg0)
	{
		switch (arg0.getId())
		{
		case R.id.edit_save:
			if (checkValidity(getView()))
			{
				Room room = getRoom();
				room.name = mTxtName.getText().toString().trim();
				room.code = mCode;
				insert(room);
			}
			break;
		case R.id.edit_del:
			Room room = getRoom();
			if (room != null)
			{
				delete(room);
			}
			break;
		default:
			break;
		}

	}

	@Override
	public boolean checkValidity(View view)
	{
		if (mTxtName == null)
			return false;
		if (mTxtName.getText().toString().trim().length() == 0)
		{
			return false;
		}
		return true;
	}

	@Override
	public void displayItem(Room item)
	{
		if (item == null || item.type == Type.ADD_ITEM || item.type == Type.ALL_ITEM)
		{
			mTxtName.setText("");
			setRoomCode(0);
			mOKBtn.setVisibility(View.VISIBLE);
			mDelBtn.setVisibility(View.GONE);
		} else if (item.type == Type.DEL_ITEM)
		{
			mOKBtn.setVisibility(View.GONE);
			mDelBtn.setVisibility(View.VISIBLE);
		} else if (item.type == Type.NORMAL_ITEM)
		{
			mTxtName.setText(item.name);
			setRoomCode(item.code);
			mOKBtn.setVisibility(View.GONE);
			mDelBtn.setVisibility(View.VISIBLE);
		}
	}

	public void onBasicItemSelected(Manager mgr, BasicItem item, boolean admin)
	{
		if (item instanceof Room)
			displayItem((Room) item);
	}

	private void setRoomCode(int code)
	{
		mCode = code;
		mTxtCode.setText(StringUtil.convRoomCode(mContext, code));
	}
}
